'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
'
'Copyright (c) 2012, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Will Strohl, Simple Redirect, nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke.Entities.Modules

NameSpace WillStrohl.Modules.SimpleRedirect

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' The ViewSimpleRedirect class displays the content
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Partial Class View
        Inherits SimpleRedirectModuleBase
        Implements IActionable

#Region " Event Handlers "

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Page_Load runs when the control is loaded
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                If Not Page.IsPostBack Then
                    BindData()
                End If

                If Not IsEditable AndAlso PortalSettings.UserMode <> Portals.PortalSettings.Mode.Edit Then
                    ContainerControl.Visible = False
                End If
            Catch exc As Exception        'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

#End Region

#Region " Helper Methods "

        Private Sub BindData()

            InitializeRedirect()

            InitializeDebug()
            
        End Sub

        Private Sub LocalizeModule()

            ' do nothing

        End Sub

        Private Sub InitializeDebug()

            Dim blnDebug As Boolean = False
            If Not Settings("Debug") Is Nothing Then
                blnDebug = Boolean.Parse(Settings("Debug"))
            End If

            If blnDebug Then

                Dim sb As New System.Text.StringBuilder
                sb.AppendFormat("<span class=""NormalBold"">Absolute Path:</span> {0}<br />", Request.Url.AbsolutePath)
                sb.AppendFormat("<span class=""NormalBold"">Absolute URI:</span> {0}<br />", Request.Url.AbsoluteUri)
                sb.AppendFormat("<span class=""NormalBold"">Absolute Authoriy:</span> {0}<br />", Request.Url.Authority)
                sb.AppendFormat("<span class=""NormalBold"">DNS Safe Host:</span> {0}<br />", Request.Url.DnsSafeHost)
                sb.AppendFormat("<span class=""NormalBold"">Fragment:</span> {0}<br />", Request.Url.Fragment)
                sb.AppendFormat("<span class=""NormalBold"">Host:</span> {0}<br />", Request.Url.Host)
                sb.AppendFormat("<span class=""NormalBold"">Local Path:</span> {0}<br />", Request.Url.LocalPath)
                sb.AppendFormat("<span class=""NormalBold"">Original String:</span> {0}<br />", Request.Url.OriginalString)
                sb.AppendFormat("<span class=""NormalBold"">Path and Query:</span> {0}<br />", Request.Url.PathAndQuery)
                sb.AppendFormat("<span class=""NormalBold"">Ports:</span> {0}<br />", Request.Url.Port)
                sb.AppendFormat("<span class=""NormalBold"">Query:</span> {0}<br />", Request.Url.Query)
                sb.AppendFormat("<span class=""NormalBold"">Scheme:</span> {0}<br />", Request.Url.Scheme)
                sb.AppendFormat("<span class=""NormalBold"">User Info:</span> {0}<br />", Request.Url.UserInfo)
                sb.AppendFormat("<span class=""NormalBold"">Application Path:</span> {0}<br />", Request.ApplicationPath)
                sb.AppendFormat("<span class=""NormalBold"">Current Execution File Path:</span> {0}<br />", Request.CurrentExecutionFilePath)
                sb.AppendFormat("<span class=""NormalBold"">File Path:</span> {0}<br />", Request.FilePath)
                sb.AppendFormat("<span class=""NormalBold"">HTTP Method:</span> {0}<br />", Request.HttpMethod)
                sb.AppendFormat("<span class=""NormalBold"">Path:</span> {0}<br />", Request.Path)
                sb.AppendFormat("<span class=""NormalBold"">Path Info:</span> {0}<br />", Request.PathInfo)
                sb.AppendFormat("<span class=""NormalBold"">Physical Application Path:</span> {0}<br />", Request.PhysicalApplicationPath)
                sb.AppendFormat("<span class=""NormalBold"">Physical Path:</span> {0}<br />", Request.PhysicalPath)
                sb.AppendFormat("<span class=""NormalBold"">Raw URL:</span> {0}<br />", Request.RawUrl)
                sb.AppendFormat("<span class=""NormalBold"">User Host Name:</span> {0}<br />", Request.UserHostName)
                sb.AppendFormat("<span class=""NormalBold"">DotNetNuke Common Application Path:</span> {0}<br />", DotNetNuke.Common.ApplicationPath)
                sb.AppendFormat("<span class=""NormalBold"">DotNetNuke Common Application URL:</span> {0}<br />", DotNetNuke.Common.ApplicationURL())
                sb.AppendFormat("<span class=""NormalBold"">DotNetNuke Portal Http Alias:</span> {0}<br />", PortalSettings.PortalAlias.HTTPAlias)
                sb.AppendFormat("<span class=""NormalBold"">Current Combined URL:</span> {0}://{1}{2}<br />", Request.Url.Scheme, PortalSettings.PortalAlias.HTTPAlias, Request.RawUrl)

                lblDebug.Text = sb.ToString()

            Else

                lblDebug.Visible = False

            End If

        End Sub

        Private Sub InitializeRedirect()

            Dim ctlModule As New RedirectController
            Dim collRedirect As RedirectInfoCollection = ctlModule.GetRedirects(ModuleId)

            If Not collRedirect Is Nothing AndAlso collRedirect.Count > 0 Then

                Dim strCurrentUrl As String = String.Concat(Request.Url.Scheme, "://", PortalSettings.PortalAlias.HTTPAlias, Request.RawUrl)

                For Each oRedirect As RedirectInfo In collRedirect

                    If String.Equals(strCurrentUrl, oRedirect.RedirectFrom) Then
                        Response.StatusCode = 301
                        Response.StatusDescription = "Moved Permanently"
                        Response.RedirectLocation = oRedirect.RedirectTo
                        HttpContext.Current.ApplicationInstance.CompleteRequest()
                    End If

                Next

            End If

        End Sub

#End Region

#Region " Optional Interfaces "

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Registers the module actions required for interfacing with the portal framework
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public ReadOnly Property ModuleActions() As Entities.Modules.Actions.ModuleActionCollection Implements Entities.Modules.IActionable.ModuleActions
            Get
                Dim Actions As New Actions.ModuleActionCollection
                Actions.Add(GetNextActionID, _
                    Localization.GetString(Entities.Modules.Actions.ModuleActionType.AddContent, LocalResourceFile), _
                    Entities.Modules.Actions.ModuleActionType.AddContent, _
                    String.Empty, String.Empty, _
                    EditUrl(String.Empty, String.Empty, "Edit"), _
                    False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)
                Return Actions
            End Get
        End Property

#End Region

    End Class

End NameSpace